﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slock.Components;
using Microsoft.Xna.Framework;
using Core;
using Slock.Generation;
using Core.Data;
using Core.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Slock
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SLockEngineGame : EngineContext
    {
        World world;

        /// <summary>
        /// Initializes a new instance os SLockEngineGame
        /// </summary>
        public SLockEngineGame() : base()
        {
            Content.RootDirectory = "SLockEngineContent";
            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            GraphicsDeviceManager.SynchronizeWithVerticalRetrace = false;
            IsFixedTimeStep = false;

            world = new World(this);

            var genrator = new FlatPlaneGenerator();

            world.GenerateWorld(genrator);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            var repo = this.Services.GetService(typeof(EngineConfigurationRepository)) as EngineConfigurationRepository;

            var input = this.Services.GetService(typeof(InputManager)) as InputManager;

            input.LoadMappingsFromIniFiles(repo.GetConfiguration("SLockControlBindings"));
        }

        /// <summary>
        /// Updates
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Update(GameTime gameTime)
        {
            world.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// Draws
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.Clear(WorldInfo.FogColor);
            world.Draw(gameTime);
            base.Draw(gameTime);
        }
    }
}
